package net;


import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * 网络编程练习-客户端
 */
public class NetExercise01Client {

    public static void main(String[] args) throws Exception {

        /*
            1)使用字符流的方式，编写一个客户端程序和服务端程序
            2)客户端发送"name"，服务端接收后，返回 "我是nova"
            3)客户端发送"hobby"，服务端接收后，返回 "编写java程序"
            4)不是这两个问题，回复"你说啥呢"
         */

        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);

        OutputStream outputStream = socket.getOutputStream();
        System.out.println("请输入问题：");
        String question = new Scanner(System.in).next();
        outputStream.write(question.getBytes());
        socket.shutdownOutput();

        InputStream inputStream = socket.getInputStream();
        byte[] buffer = new byte[1024];
        int readLen;
        while ((readLen = inputStream.read(buffer)) != -1) {
            System.out.println("服务端回复：" + new String(buffer, 0, readLen));
        }

        inputStream.close();
        outputStream.close();
        socket.close();
        System.out.println("客户端退出...");
    }
}
